
#ifndef _C_THREAD_H_
#define _C_THREAD_H_

#include <cTools/Common.h>
#include <pthread.h>

struct _C_Thread;
struct _C_Thread_Args;

typedef void (*C_Thread_Cleanup_t)(struct _C_Thread*);
typedef void (*C_Thread_Run_t)(struct _C_Thread*);

/** Thread flags (for internal usage) **/
#define C_THREAD_FLAG_STARTED	(1 << 0)
#define C_THREAD_FLAG_DETACHED	(1 << 1)

typedef struct _C_Thread_Args
{
	struct _C_Thread*	Thread;
	void*				Args;
} C_Thread_Args;

typedef struct _C_Thread
{
	pthread_t			Handle;
	uint32_t			Flags;
	C_Bool_t			Run;
	C_Thread_Run_t		Function;
	void*				User;
	C_Thread_Cleanup_t	Cleanup;
} C_Thread;

typedef C_Thread* C_Thread_Ptr;

C_Thread_Ptr
C_Thread_New(C_Thread_Run_t);

void
C_Thread_Init(C_Thread_Ptr, C_Thread_Run_t);

void
C_Thread_Base_Cleanup(C_Thread_Ptr);

void
C_Thread_Cleanup(C_Thread_Ptr);

C_Error_t
C_Thread_Run(C_Thread_Ptr, C_Bool_t, void*);

void
C_Thread_Set_Cleanup(C_Thread_Ptr, C_Thread_Cleanup_t);

C_Error_t
C_Thread_Stop(C_Thread_Ptr);

void
C_Thread_Free(C_Thread_Ptr);

#define C_THREAD_SET_CLEANUP(t, f) \
	C_Thread_Set_Cleanup((C_Thread_Ptr)t, (C_Thread_Cleanup_t)f)

#define C_THREAD_STOP(t) \
	C_Thread_Stop((C_Thread_Ptr)t)

#define C_THREAD_FREE(t) \
	C_Thread_Free((C_Thread_Ptr)t)

#endif
